package com.rowline.admin.module.study.mybatiscache;

import org.apache.ibatis.session.SqlSession;

import java.sql.SQLException;

/**
 * ClassName:MybatisCacheDemo01
 * Created by 245167
 * on 2018/4/28  28:53
 *    结果分析：在第一次查询id为1的employee对象时执行了一条select语句，接下来执行了一个delete并commit操作，
 * Mybatis为了保证缓存中存储的是最新消息，会清空SqlSession缓存。当第二次获取id为1的User对象时一级缓存也就是
 * SqlSession缓存中并没有缓存任何对象，所以Mybatis再次执行语句去查询id为1的Exployee对象。
 */
public class MybatisCacheDemo02 {

    public static void main(String[] args) {
        // 使用工厂类获得SqlSession对象
        SqlSession sqlSession = MySqlSessionFactory.getSqlSession();
        // 获得InterAgencyMapper对象
        InterAgencyMapper interAgencyMapper = sqlSession.getMapper(InterAgencyMapper.class);
        Long id = 5L;
        InterAgencyInfo agencyInfo = null;

        try {
            agencyInfo = interAgencyMapper.queryAgencyInfoById(id);
            System.out.println("1 - 查询到数据为 ：" + agencyInfo.toString());
        } catch (SQLException e) {
            e.printStackTrace();
        }

        Long id2 = 6L;
        try {
            interAgencyMapper.deleteAgencyInfoById(id2);
            System.out.println("2 - 删除ID ：" + id2 + " 的记录");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        sqlSession.commit();

        try {
            agencyInfo = interAgencyMapper.queryAgencyInfoById(id);
            System.out.println("3 - 查询到数据为 ：" + agencyInfo.toString());
        } catch (SQLException e) {
            e.printStackTrace();
        }

        sqlSession.close();

    }


}
